package locality;

import java.util.ArrayList;
import java.io.*;

import mantel.mantelTest;

import tool.Utility;

import genome.BG;
import genome.CGP;
import genome.Genotype;

public class CoSymExperiment {
	static double[] input = {0.0};
	static int NODES = 5;
	static int SIZE = NODES*3;
	static int samples = 1000;
	
	public static void main(String args[]) throws IOException{
		//symbolicOutcomes();
		//cgpCorrelogram();
		bgCorrelogram();
	}

	private static void bgCorrelogram() throws IOException{
		
		for (int i = 0; i < 10; i++) {
		
		BG x = new BG(SIZE);
		ArrayList<Genotype> space = x.generateSpace(samples);
		distanceMapGenerator dmg = new distanceMapGenerator(space, 1);
		ArrayList<double[][]> bgDistanceMatrix = dmg.genDMatrix();
		double[][] X = bgDistanceMatrix.get(0);
		double[][] Y = bgDistanceMatrix.get(1);
		
		/*PrintWriter p = new PrintWriter(new FileWriter(new File("dPlot.txt")));
		double[] Xv = Utility.distMatrixToVector(X);
		double[] Yv = Utility.distMatrixToVector(Y);
		for (int i = 0; i < Yv.length; i++) {
			System.out.println(Xv[i]+" "+Yv[i]);
			p.println(Xv[i]+" "+Yv[i]);
		}
		p.close();*/

			double[][] correlogram = mantelTest.segmented_mantel_cor(X, Y, 1000, 2);
			Utility.dump("bicor"+i+"_correlog.dat", correlogram);
			mantelTest.printCorrelog(correlogram);
		}
		
	}
	
	private static void cgpCorrelogram(){
		CGP x = new CGP(input,SIZE);
		ArrayList<Genotype> space = x.generateSpace(samples);
		distanceMapGenerator dmg = new distanceMapGenerator(space, 0);
		ArrayList<double[][]> cgpDistanceMatrix = dmg.genDMatrix();
		double[][] X = cgpDistanceMatrix.get(0);
		double[][] Y = cgpDistanceMatrix.get(1);
		
		double[][] correlogram = mantelTest.segmented_mantel_cor(X, Y, 1000, 100);
		mantelTest.printCorrelog(correlogram);
	}
	
	private static void symbolicOutcomes() {
		CGP x = new CGP(input,SIZE);
		CoSymbolicRegression csr = new CoSymbolicRegression(x,x);
		ArrayList<Genotype> space = x.generateSpace();
		System.out.println(space);
		
		for (Genotype s: space){
			System.out.println(s.toMathString());
		}
		
		double[][] outcomes = new double[space.size()][space.size()];
		int i=0; int j=0;
		for(Genotype g1: space){
			for(Genotype g2: space){
				csr.setG1(g1);
				csr.setG2(g2);
				outcomes[i][j] = csr.CO1();
				j = j+1;
			}
			i = i+1;
			j = 0;
		}
		
		Utility.printMat(outcomes);
		Utility.dump("./symbolic_out.dat", outcomes);
	}
}
